package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/15 0015.
 */

public class UpnickNameActivity extends BaseActivity implements View.OnClickListener{

    private ImageView upnickName_back;
    private EditText upnickName_et;
    private TextView upnickName_config;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upnikname);
        initView();
    }
    private void initView(){
        upnickName_back = (ImageView) findViewById(R.id.upnickName_back);
        upnickName_back.setOnClickListener(this);
        upnickName_et = (EditText) findViewById(R.id.upnickName_et);
        upnickName_config = (TextView) findViewById(R.id.upnickName_config);
        upnickName_config.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.upnickName_back:
                finish();
                break;
            case R.id.upnickName_config:
                upNikeName();
                break;
        }
    }

    private void upNikeName()
    {
        final String name = upnickName_et.getText().toString().trim();
        if (name.isEmpty()){
            showToast("请输入昵称");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user", MyApp.userData.getData().getUser_id());
        map.put("nick_name", name);
        XUtil.Post(UrlConfig.UPNIKENAME_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("修改昵称result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    MyApp.userData.getData().setNick_name(name);
                    UpnickNameActivity.this.setResult(Constants.RESULT_DATA);
                    UpnickNameActivity.this.finish();
                    MyApp.isRefresh = true;
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("修改昵称失败");
            }
        });

    }
}
